<template>
	<view class="spwd">
		<view class="spwd-input">
			<input type="text" v-model="username" placeholder="账号"/>
		</view>
		<view class="spwd-input">
			<input type="password" v-model="password" placeholder="密码"/>
		</view>
		<view>
			<button @click="login">登录</button>
		</view>
		<view class="forget" @click="forget">
			<span>忘记密码?</span>
		</view>
	</view>
</template>

<script>
	import * as api from '../config/api.js'
	export default {
		data() {
			return {
				username:'',
				password:''
			}
		},
		onLoad() {
			let leaveUserNum = uni.getStorageSync('leaveUserNum')
			// console.log(leaveUserNum)
			// 有缓存就不进入登陆界面了
			if(leaveUserNum != ''){
				uni.switchTab({
				    url: '/pages/tabbar/tab_index'
				});
			}
		},
		methods:{
			setUserSession(leaveUserNum){
				api.getUserByIdApi(leaveUserNum,{
					data:{},
					success:res=>{
						console.log(res)
					},
					fail:err=>{
						uni.showToast({
							title:"网络延迟",
							icon:"none"
						})
					}
				})
			},
			login() {
				let that = this
				let leaveUserNum = this.username
				let leaveUserPwd = this.password
				// console.log("输入账号:"+leaveUserNum)
				// console.log("输入密码:"+leaveUserPwd)
				if(leaveUserNum == '' || leaveUserPwd == ''){
					uni.showToast({
						title:"账号密码不可为空",
						icon:"none"
					})
				}else{
					// uni.switchTab({
					// 	url: '/pages/tabbar/tab_index',
					// 	success(res) {
					// 		uni.setStorageSync('leaveUserNum',leaveUserNum)
					// 	}
					// });
					api.appLoginApi(leaveUserNum,leaveUserPwd,{
						data:{},
						success:res => {
							console.log(res)
							if(res.code == 200){
								// console.log("成功")
								
								// 获取用户id
								let leaveUserNum = res.data.leaveUserNum;
								// that.setUserSession(leaveUserNum);
								let leaveUser = res.data
								// console.log("用户账号"+leaveUserNum)
								// 存储用户NUM 并跳转界面
								uni.switchTab({
									url: '/pages/tabbar/tab_index',
									success(res) {
										// 缓存存储  登陆信息
										uni.setStorageSync('leaveUserNum',leaveUserNum)
										// uni.setStorageSync('leaveUser',leaveUser)
										that.$store.commit('setLeaveUserNum',leaveUserNum)
										that.$reoladUser();
										// 模拟存储token
										// uni.setStorageSync('token',1)
										// that.$store.commit('setToken',1)
										
										// 打印信息
										// console.log(that.$store.getters.getLeaveUserNum)
										// console.log( uni.getStorageSync('leaveUserNum'))
										// console.log(that.$store.getters.getToken)
									}
								});
							}else if(res.code == 500){
								uni.showToast({
									title:"账号或者密码错误",
									icon:"none"
								})
							}else{
								uni.showToast({
									title:"登陆失败",
									icon:"none"
								})
							}
						},
						fail:err=>{
							uni.showToast({
								title:"网络延迟",
								icon:"none"
							})
						}
					});
				};
			},
			forget() {
				uni.showToast({
					title: '请联系老师或管理员重置密码',
					icon: 'none'
				}); 
			},
		},
	}
</script>

<style lang="css">
	.spwd-input:first-child{
		margin-top: 10%;
	}
	.spwd-input{
		border-bottom:1rpx solid #ababab;
		width: 90%;
		margin: auto;
	}
	
	.spwd-input input{
		height: 12vw;
		font-size: 120%;
	}
	
	.spwd-input:hover{
		border-bottom:5rpx solid rgba(70,148,255,1);
	}
	
	.spwd button{
		margin-top: 10%;
		width: 80%;
		background-color: rgba(70,148,255,1);
		border: 0;
		color: #ffffff;
	}
	
	.forget{
		margin-top: 7%;
		text-align: center;
		color: #3b3b3b;
		font-weight: 900;
		font-size: 110%;
	}
	
	.forget:hover{
		color: #717171;
	}
</style>